package com.jesterplayer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;


public class CurrentSongActivity extends Activity implements OnGestureListener {
	
	private GestureDetector gDetector;
	private static PlaybackController pc = new PlaybackController();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//CurrentTrack  ct= new CurrentTrack(Environment.getExternalStorageDirectory().getPath().replace("sdcard0", "sdcard1") + "/Music/Reverie.mp3");
		gDetector = new GestureDetector(this, this, null);
		
		//pc.start();
		System.out.println("pcstarted");
		
		setContentView(R.layout.activity_current_song);
		getActionBar().hide();
		
		//Log.d("FILE", Environment.getExternalStorageDirectory().toString());
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_current_song, menu);
		return true;
	}
	
	
	

	public void goToLibrary(View view) {
		
		
		
		Intent intent = new Intent(this, LibraryActivity.class);
		//intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		
		startActivity(intent);
		overridePendingTransition(R.animator.activity_pull_down,
				R.animator.activity_push_down);
	}
	
	/**
	 * Call this method when wanting to go to the now playing list
	 * 
	 * @param view - the view to go to
	 */
	public void goToNowPlaying(View view) {
		
		
		Intent intent = new Intent(this, NowPlayingActivity.class);
		
		startActivity(intent);
		overridePendingTransition(R.animator.activity_pull_up,
				R.animator.activity_push_up);
	}

	// Overloads the touch allowing gestures
	@Override
	public boolean onTouchEvent(MotionEvent me) {
		return gDetector.onTouchEvent(me);
	}
	
	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		float absVelX = velocityX;
		float absVelY = velocityY;
		if (absVelX < 0)
			absVelX *= -1;
		if (absVelY < 0)
			absVelY *= -1;

		if (absVelX > absVelY) {
			if (velocityX < 0){
				pc.nextSong();
				Log.d("Fling", "next");}
			else{
				pc.previousSong();
				Log.d("Fling", "previous");
			}

		} else {
			// Vertical Fling
			if (velocityY < 0){
				
				goToNowPlaying(null);}
			else
				goToLibrary(null);
		}
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		 
		// Vibrate for 300 milliseconds
		v.vibrate(150);

		PlaybackController.toggleShuffle();
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		pc.togglePlayback();
		return false;
	}

}
